package com.huike.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huike.constants.MessageConstant;
import com.huike.dto.LogPageDto;
import com.huike.entity.Clue;
import com.huike.entity.SysOperLog;
import com.huike.mapper.SysOperLogMapper;
import com.huike.result.PageResult;
import com.huike.result.Result;
import com.huike.service.SysLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.Date;

@Service
public class SysLogServiceImpl implements SysLogService {
    @Autowired
    private SysOperLogMapper sysOperLogMapper;
    @Override
    public Result listLog(LogPageDto dto) {
        if (dto == null) {
            return Result.error(MessageConstant.DATA_NOT_EXIST);
        }
        IPage<SysOperLog> pageResult = new Page<>(dto.getPage(), dto.getPageSize());
        LambdaQueryWrapper<SysOperLog> lqw = new LambdaQueryWrapper<>();

        if (dto.getWord() != null){
            lqw.like(SysOperLog::getOperUrl,dto.getWord()).or()
                    .like(SysOperLog::getTitleName,dto.getWord()).or()
                    .like(SysOperLog::getRequestMethod,dto.getWord()).or()
                    .like(SysOperLog::getOperName,dto.getWord());

        }


        Date serachTime = dto.getSearchTime();
        if (serachTime!=null){
            Instant instant = serachTime.toInstant();
            // 将 Instant 转换为 LocalDateTime
            LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
            LocalDateTime minDateTime = localDateTime.with(LocalTime.MIN);
            LocalDateTime maxDateTime = localDateTime.with(LocalTime.MAX);
            lqw.le(SysOperLog::getOperTime, maxDateTime)
                    .ge(SysOperLog::getOperTime, minDateTime);
        }

        sysOperLogMapper.selectPage(pageResult, lqw);
        PageResult result = new PageResult(pageResult.getTotal(), pageResult.getRecords());


        return Result.success(result);

    }
}
